<template>
  <div>
     <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>分类参数</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 警告区域 -->
       <el-alert
        title="注意：此处只能为三级分类设置相关参数"
        type="warning"
        :closable="false"
        show-icon>
       </el-alert>
       <!-- 选择商品分类区域 -->
       <el-row class="classOpt">
         <el-col>
           <span>选择商品分类:</span>
           <el-cascader
                v-model="selectClass"
                :props="classParam"
                :options="ClassList"
                expand-trigger="hover"
                @change="changeState">
           </el-cascader>
         </el-col>
       </el-row>
       <!-- tab页签区域 -->
         <el-tabs v-model="activeName" @tab-click="changeTab">
           <!-- 动态参数区域 -->
            <el-tab-pane label="动态参数" name="Dynamics">
              <el-button type="primary" size="mini" :disabled="isTabUsed" @click="addClassParams = true">添加参数</el-button>
              <!-- 动态参数表格 -->
              <el-table border :data="dynamicsList">
                <el-table-column type="expand">
                  <template slot-scope="scope">
                    <!-- 渲染动态参数 -->
                    <el-tag closable @close="handClose(i,scope.row)" v-for="(item, i) in scope.row.desc" :key="i">{{ item }}</el-tag>
                    <!-- 增加动态参数 -->
                    <el-input
                      class="input-new-tag"
                      v-if="scope.row.inputVisible"
                      v-model="scope.row.inputValue"
                      ref="saveTagInput"
                      size="small"
                      @keyup.enter.native="handleInputConfirm(scope.row)"
                      @blur="handleInputConfirm(scope.row)"
                    >
                    </el-input>
                  <el-button v-else class="button-new-tag" size="small" @click="showInput(scope.row)">+ New Tag</el-button>
                  </template>
                </el-table-column>
                <el-table-column type="index" label="#"></el-table-column>
                <el-table-column  label="参数名称" prop="class"></el-table-column>
                <el-table-column  label="操作">
                  <template>
                    <el-button type="primary" icon="el-icon-edit" size="mini" >编辑</el-button>
                    <el-button type="danger" icon="el-icon-delete" size="mini" >删除</el-button>
                  </template>
                </el-table-column>
              </el-table>
            </el-tab-pane>
          <!-- 静态属性区域 -->
            <el-tab-pane label="静态属性" name="States">
              <el-button type="primary" size="mini" :disabled="isTabUsed" @click="addClassParams = true">添加属性</el-button>
              <!-- 静态属性表格 -->
               <el-table border :data="stateList">
                <el-table-column type="expand">
                  <template slot-scope="scope">
                    <el-tag >{{ scope.row.system }}</el-tag>
                    <el-tag >{{ scope.row.apparent }}</el-tag>
                  </template>
                </el-table-column>
                <el-table-column type="index" label="#"></el-table-column>
                <el-table-column  label="属性名称" prop="iteam"></el-table-column>
                <el-table-column  label="操作">
                  <template>
                    <el-button type="primary" icon="el-icon-edit" size="mini" >编辑</el-button>
                    <el-button type="danger" icon="el-icon-delete" size="mini" >删除</el-button>
                  </template>
                </el-table-column>
              </el-table>
            </el-tab-pane>
         </el-tabs>
    </el-card>
    <!-- 添加分类弹窗 -->
     <el-dialog
        :title="textTitle"
        :visible.sync="addClassParams"
        width="40%"
        @close="addClassClose"
        >
        <!-- 添加信息 -->
        <el-form ref="addClassParamsRef" :model="ClassParams" :rules="paramsRules" label-width="80px">
          <el-form-item :label="textTitle" prop="name">
            <el-input v-model="ClassParams.name" ></el-input>
            </el-form-item>
        </el-form>
          <!-- 底部按钮 -->
          <span slot="footer" class="dialog-footer">
          <el-button @click="addClassParams = false">取 消</el-button>
          <el-button @click="addClass" type="primary" >确 定</el-button>
          </span>
      </el-dialog>
  </div>
</template>
<script>
export default {
  data () {
    return {
      ClassList: [],
      classParam: {
        value: '_id',
        label: 'label',
        children: 'children'
      },
      selectClass: [],
      activeName: 'Dynamics',
      addClassParams: false,
      paramsRules: {
        label: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      ClassParams: {
        name: ''
      },
      stateList: [],
      dynamicsList: []
    }
  },
  created () {
    this.getClassList()
  },
  methods: {
    async getClassList () {
      const res = await this.$http.get('/product')
      console.log(res)
      this.ClassList = res.data.list
    },
    changeState () {
      this.getParamList()
    },
    async getParamList () {
      const res = await this.$http.get(`/class/${this.createId}/attributes`, {
        params: { sel: this.activeName }
      })
      if (res.data.doc.dynamics[0]._id) {
        res.data.doc.dynamics.forEach(item => {
          item.desc = item.desc.split('，')
          item.inputVisible = false
          item.inputValue = ''
        })
      }
      console.log(res.data.doc.dynamics[0]._id)
      this.stateList = res.data.doc.states
      this.dynamicsList = res.data.doc.dynamics
    },
    changeTab () {
      this.getParamList()
      console.log(this.activeName)
    },
    async addClass () {
      this.$refs.addClassParamsRef.validate(valid => {
        if (!valid) return this.$message.error('请输入合法字符！')
        this.addClassParams = false
        this.$message.success('添加成功！')
      })
    },
    addClassClose () {
      this.$refs.addClassParamsRef.resetFields()
    },
    // 失去焦点按下回车触发Tags
    handleInputConfirm (row) {
      if (row.inputValue.trim().length === 0) {
        row.inputValue = ''
        row.inputVisible = false
      }
      row.desc.push(row.inputValue.trim())
      row.inputValue = ''
      row.inputVisible = false
      this.saveTag(row)
    },
    // 保存Tag状态
    async saveTag (row) {
      await this.$http.put(`/class/${this.createId}/attribute/${row._id}`, {
        params: {
          class: row.class,
          desc: row.desc.join('，')
        }
      })
    },
    // 点击按钮隐藏输入框
    showInput (row) {
      row.inputVisible = true
      // 下一帧
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },
    // 删除Tag标签
    handClose (i, row) {
      row.desc.splice(i, 1)
      this.saveTag(row)
    }
  },
  computed: {
    // 控制按钮是否激活
    isTabUsed () {
      if (this.selectClass.length !== 3) {
        return true
      }
      return false
    },
    // 创建三级分类
    createId () {
      if (this.selectClass.length === 3) {
        return this.selectClass[2]
      }
      return null
    },
    // 动态加载弹窗属性
    textTitle () {
      if (this.activeName === 'first') {
        return '添加参数'
      }
      return '添加属性'
    }
  }
}
</script>
<style lang="less" scoped>
.classOpt {
  margin: 15px 0 ;
  span {
    margin-right:10px;
  }
}
.el-tag {
  margin:10px;
}
.input-new-tag {
  width:120px;
}
</style>
